{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/v-fech/anaconda3/lib/python3.6/site-packages/statsmodels/compat/pandas.py:56: FutureWarning: The pandas.core.datetools module is deprecated and will be removed in a future version. Please use the pandas.tseries module instead.\n",
      "  from pandas.core import datetools\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import os\n",
    "import sys\n",
    "import scipy\n",
    "import statsmodels.api as sm\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "N_stocks=50\n",
    "N_features=5\n",
    "N_T=300\n",
    "X=np.load(\"./data/simulate_feature.npy\")\n",
    "# X=np.transpose(X,(2,0,1))\n",
    "X=np.transpose(X,(1,2,0))\n",
    "R=np.load(\"./data/simulate_return.npy\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(300, 50)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "R.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "'''\n",
    "mod: model\n",
    "res: fitting result\n",
    "spec_ret: specific return on each stock without adjustments\n",
    "factor_ret: factor return on panel data of time T.\n",
    "X: factor Weight\n",
    "R: return of stock\n",
    "'''\n",
    "\n",
    "\n",
    "mod={}\n",
    "res={}\n",
    "factor_return={}\n",
    "spec_ret={}\n",
    "# Regress stock return to feature on panel\n",
    "for time_step in range(N_T):\n",
    "    mod[time_step] = sm.OLS(R[time_step,:],X[time_step,:,:])\n",
    "    res[time_step] = mod[time_step].fit()\n",
    "    factor_return[time_step] = res[time_step].params\n",
    "    # ?sm.regression.linear_model.RegressionResults\n",
    "    spec_ret[time_step]=R[time_step,:]-res[time_step].predict(X[time_step,:,:])\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.0011667 , -0.00183906,  0.00161737, ..., -0.00015827,\n",
       "         0.00044379, -0.00138426],\n",
       "       [-0.00085246,  0.00035822,  0.00050521, ...,  0.00012792,\n",
       "        -0.00187362,  0.00125096],\n",
       "       [-0.00049246, -0.00052524, -0.00084366, ...,  0.0001269 ,\n",
       "         0.00012323,  0.00035079],\n",
       "       ...,\n",
       "       [-0.00022191,  0.00198432, -0.00153041, ...,  0.00045411,\n",
       "        -0.00021368,  0.00183621],\n",
       "       [ 0.00032668,  0.00099936, -0.00051045, ..., -0.00121134,\n",
       "         0.00125261,  0.00142306],\n",
       "       [ 0.0008879 ,  0.00133383,  0.00076555, ..., -0.00182932,\n",
       "         0.00053263,  0.00156119]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'''\n",
    "tau_sigma_spec_return obtained by Newey-West:\n",
    "'''\n",
    "\n",
    "tau_specific_ret=100\n",
    "\n",
    "spec_ret_nw=pd.DataFrame(spec_ret).T.values\n",
    "spec_ret_nw\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "'''\n",
    "tau_corr & tau_sigma obtained by Newey-West test.\n",
    "\n",
    "'''\n",
    "\n",
    "tau_corr=100\n",
    "tau_sigma=10\n",
    "\n",
    "tau_specific_ret=100\n",
    "\n",
    "df_factor_return=pd.DataFrame(factor_return).T\n",
    "\n",
    "'''\n",
    "Seperate covariance matrix into correlation matrix and volatility of differnent time dynamics(aka the lags/ half time of autocorrelation are different.)\n",
    "\n",
    "then we treat volatility and correlation matrix seperately.\n",
    "'''\n",
    "\n",
    "\n",
    "'''\n",
    "todo:\n",
    "\n",
    "add auto newey-west lag selection:\n",
    "\n",
    "refer to https://github.com/statsmodels/statsmodels/edit/master/statsmodels/stats/sandwich_covariance.py\n",
    "\n",
    "\n",
    "'''\n",
    "corr_mv_window=df_factor_return.ewm(halflife=tau_corr)\n",
    "sigma_mv_window=df_factor_return.ewm(halflife=tau_sigma)\n",
    "corr_raw=corr_mv_window.corr()\n",
    "sigma_raw=np.sqrt(sigma_mv_window.var())\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "      <th>4</th>\n",
       "      <th>5</th>\n",
       "      <th>6</th>\n",
       "      <th>7</th>\n",
       "      <th>8</th>\n",
       "      <th>9</th>\n",
       "      <th>...</th>\n",
       "      <th>290</th>\n",
       "      <th>291</th>\n",
       "      <th>292</th>\n",
       "      <th>293</th>\n",
       "      <th>294</th>\n",
       "      <th>295</th>\n",
       "      <th>296</th>\n",
       "      <th>297</th>\n",
       "      <th>298</th>\n",
       "      <th>299</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-0.001167</td>\n",
       "      <td>-0.000852</td>\n",
       "      <td>-0.000492</td>\n",
       "      <td>-0.000795</td>\n",
       "      <td>-0.001282</td>\n",
       "      <td>-0.000203</td>\n",
       "      <td>-0.000213</td>\n",
       "      <td>-0.000801</td>\n",
       "      <td>-0.000645</td>\n",
       "      <td>0.000184</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.000085</td>\n",
       "      <td>0.000900</td>\n",
       "      <td>-0.000474</td>\n",
       "      <td>-0.002101</td>\n",
       "      <td>0.000689</td>\n",
       "      <td>0.001434</td>\n",
       "      <td>-0.000477</td>\n",
       "      <td>-0.000222</td>\n",
       "      <td>0.000327</td>\n",
       "      <td>0.000888</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-0.001839</td>\n",
       "      <td>0.000358</td>\n",
       "      <td>-0.000525</td>\n",
       "      <td>-0.000972</td>\n",
       "      <td>0.000426</td>\n",
       "      <td>-0.000366</td>\n",
       "      <td>-0.000932</td>\n",
       "      <td>-0.000329</td>\n",
       "      <td>-0.000403</td>\n",
       "      <td>0.001878</td>\n",
       "      <td>...</td>\n",
       "      <td>0.001952</td>\n",
       "      <td>-0.001530</td>\n",
       "      <td>-0.000582</td>\n",
       "      <td>-0.001114</td>\n",
       "      <td>-0.000631</td>\n",
       "      <td>0.000136</td>\n",
       "      <td>-0.000608</td>\n",
       "      <td>0.001984</td>\n",
       "      <td>0.000999</td>\n",
       "      <td>0.001334</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.001617</td>\n",
       "      <td>0.000505</td>\n",
       "      <td>-0.000844</td>\n",
       "      <td>0.000573</td>\n",
       "      <td>0.000859</td>\n",
       "      <td>-0.001510</td>\n",
       "      <td>-0.000200</td>\n",
       "      <td>-0.000161</td>\n",
       "      <td>-0.000136</td>\n",
       "      <td>-0.000521</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.000393</td>\n",
       "      <td>-0.001900</td>\n",
       "      <td>-0.000458</td>\n",
       "      <td>0.000940</td>\n",
       "      <td>0.000022</td>\n",
       "      <td>-0.000768</td>\n",
       "      <td>-0.000272</td>\n",
       "      <td>-0.001530</td>\n",
       "      <td>-0.000510</td>\n",
       "      <td>0.000766</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-0.001965</td>\n",
       "      <td>-0.000314</td>\n",
       "      <td>-0.000015</td>\n",
       "      <td>-0.002073</td>\n",
       "      <td>0.001060</td>\n",
       "      <td>-0.000386</td>\n",
       "      <td>0.001254</td>\n",
       "      <td>0.001901</td>\n",
       "      <td>-0.000897</td>\n",
       "      <td>-0.000069</td>\n",
       "      <td>...</td>\n",
       "      <td>0.000017</td>\n",
       "      <td>-0.000286</td>\n",
       "      <td>-0.000736</td>\n",
       "      <td>0.000873</td>\n",
       "      <td>-0.000074</td>\n",
       "      <td>0.001166</td>\n",
       "      <td>0.000495</td>\n",
       "      <td>0.000707</td>\n",
       "      <td>-0.000756</td>\n",
       "      <td>-0.002901</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-0.000329</td>\n",
       "      <td>0.000325</td>\n",
       "      <td>0.000001</td>\n",
       "      <td>-0.000809</td>\n",
       "      <td>-0.000736</td>\n",
       "      <td>-0.000781</td>\n",
       "      <td>0.000654</td>\n",
       "      <td>0.001530</td>\n",
       "      <td>0.000205</td>\n",
       "      <td>-0.001387</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.000206</td>\n",
       "      <td>-0.000706</td>\n",
       "      <td>0.001770</td>\n",
       "      <td>0.001336</td>\n",
       "      <td>-0.002049</td>\n",
       "      <td>-0.000436</td>\n",
       "      <td>-0.000529</td>\n",
       "      <td>0.000222</td>\n",
       "      <td>0.000522</td>\n",
       "      <td>-0.001656</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>0.000937</td>\n",
       "      <td>0.000211</td>\n",
       "      <td>-0.000725</td>\n",
       "      <td>-0.000073</td>\n",
       "      <td>0.001798</td>\n",
       "      <td>0.000430</td>\n",
       "      <td>0.000324</td>\n",
       "      <td>-0.002008</td>\n",
       "      <td>-0.000065</td>\n",
       "      <td>0.000197</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.000548</td>\n",
       "      <td>-0.000935</td>\n",
       "      <td>0.000997</td>\n",
       "      <td>-0.001046</td>\n",
       "      <td>0.000562</td>\n",
       "      <td>-0.000464</td>\n",
       "      <td>-0.000412</td>\n",
       "      <td>0.000817</td>\n",
       "      <td>-0.000229</td>\n",
       "      <td>-0.000322</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>0.001466</td>\n",
       "      <td>0.000414</td>\n",
       "      <td>-0.000467</td>\n",
       "      <td>0.000546</td>\n",
       "      <td>-0.000096</td>\n",
       "      <td>-0.000146</td>\n",
       "      <td>-0.001521</td>\n",
       "      <td>0.001587</td>\n",
       "      <td>-0.001638</td>\n",
       "      <td>-0.000982</td>\n",
       "      <td>...</td>\n",
       "      <td>0.001106</td>\n",
       "      <td>-0.000414</td>\n",
       "      <td>0.000603</td>\n",
       "      <td>-0.000837</td>\n",
       "      <td>-0.000255</td>\n",
       "      <td>-0.000541</td>\n",
       "      <td>0.000804</td>\n",
       "      <td>-0.001421</td>\n",
       "      <td>-0.001140</td>\n",
       "      <td>-0.000575</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>0.000531</td>\n",
       "      <td>0.000220</td>\n",
       "      <td>-0.001428</td>\n",
       "      <td>-0.000380</td>\n",
       "      <td>-0.000647</td>\n",
       "      <td>0.000736</td>\n",
       "      <td>0.001520</td>\n",
       "      <td>-0.000819</td>\n",
       "      <td>-0.000967</td>\n",
       "      <td>0.000404</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.000019</td>\n",
       "      <td>0.000467</td>\n",
       "      <td>-0.000768</td>\n",
       "      <td>0.000021</td>\n",
       "      <td>0.000491</td>\n",
       "      <td>-0.003098</td>\n",
       "      <td>-0.000444</td>\n",
       "      <td>0.002383</td>\n",
       "      <td>-0.001050</td>\n",
       "      <td>-0.000303</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>-0.001787</td>\n",
       "      <td>0.001369</td>\n",
       "      <td>-0.001070</td>\n",
       "      <td>0.000125</td>\n",
       "      <td>-0.000637</td>\n",
       "      <td>0.000097</td>\n",
       "      <td>0.000691</td>\n",
       "      <td>-0.001382</td>\n",
       "      <td>-0.000948</td>\n",
       "      <td>0.000156</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.000495</td>\n",
       "      <td>0.000148</td>\n",
       "      <td>0.000771</td>\n",
       "      <td>0.000217</td>\n",
       "      <td>-0.000152</td>\n",
       "      <td>-0.001335</td>\n",
       "      <td>0.001246</td>\n",
       "      <td>0.000815</td>\n",
       "      <td>-0.000191</td>\n",
       "      <td>0.000051</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>0.000888</td>\n",
       "      <td>-0.000658</td>\n",
       "      <td>-0.000056</td>\n",
       "      <td>-0.000202</td>\n",
       "      <td>0.000617</td>\n",
       "      <td>-0.000344</td>\n",
       "      <td>0.000730</td>\n",
       "      <td>-0.000287</td>\n",
       "      <td>0.000702</td>\n",
       "      <td>-0.001642</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.000976</td>\n",
       "      <td>0.001061</td>\n",
       "      <td>-0.000683</td>\n",
       "      <td>0.000801</td>\n",
       "      <td>0.001206</td>\n",
       "      <td>-0.000838</td>\n",
       "      <td>0.000649</td>\n",
       "      <td>0.000011</td>\n",
       "      <td>-0.001107</td>\n",
       "      <td>0.001762</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>-0.000045</td>\n",
       "      <td>0.000936</td>\n",
       "      <td>-0.001420</td>\n",
       "      <td>-0.000151</td>\n",
       "      <td>-0.000109</td>\n",
       "      <td>0.000317</td>\n",
       "      <td>0.002315</td>\n",
       "      <td>-0.000189</td>\n",
       "      <td>0.001648</td>\n",
       "      <td>-0.002117</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.000218</td>\n",
       "      <td>0.000849</td>\n",
       "      <td>-0.001876</td>\n",
       "      <td>0.000900</td>\n",
       "      <td>-0.000092</td>\n",
       "      <td>0.000237</td>\n",
       "      <td>-0.001062</td>\n",
       "      <td>0.000570</td>\n",
       "      <td>-0.000171</td>\n",
       "      <td>-0.000584</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>-0.001345</td>\n",
       "      <td>0.000046</td>\n",
       "      <td>0.001301</td>\n",
       "      <td>0.001642</td>\n",
       "      <td>-0.000205</td>\n",
       "      <td>-0.000573</td>\n",
       "      <td>-0.000580</td>\n",
       "      <td>0.002273</td>\n",
       "      <td>-0.000138</td>\n",
       "      <td>-0.001139</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.001405</td>\n",
       "      <td>-0.001263</td>\n",
       "      <td>0.000004</td>\n",
       "      <td>0.001323</td>\n",
       "      <td>0.002033</td>\n",
       "      <td>0.000830</td>\n",
       "      <td>0.001080</td>\n",
       "      <td>-0.001044</td>\n",
       "      <td>-0.000881</td>\n",
       "      <td>-0.000020</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>0.000168</td>\n",
       "      <td>0.000572</td>\n",
       "      <td>0.000344</td>\n",
       "      <td>-0.000703</td>\n",
       "      <td>0.001349</td>\n",
       "      <td>0.001633</td>\n",
       "      <td>-0.000863</td>\n",
       "      <td>-0.000350</td>\n",
       "      <td>-0.000221</td>\n",
       "      <td>0.000981</td>\n",
       "      <td>...</td>\n",
       "      <td>0.000253</td>\n",
       "      <td>0.001538</td>\n",
       "      <td>0.000803</td>\n",
       "      <td>0.001599</td>\n",
       "      <td>-0.001355</td>\n",
       "      <td>0.000827</td>\n",
       "      <td>-0.000951</td>\n",
       "      <td>-0.000623</td>\n",
       "      <td>0.000734</td>\n",
       "      <td>-0.000667</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>0.000254</td>\n",
       "      <td>0.000065</td>\n",
       "      <td>-0.000256</td>\n",
       "      <td>-0.000456</td>\n",
       "      <td>0.000140</td>\n",
       "      <td>0.001723</td>\n",
       "      <td>0.000759</td>\n",
       "      <td>0.001423</td>\n",
       "      <td>-0.000183</td>\n",
       "      <td>-0.001370</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.000357</td>\n",
       "      <td>0.000292</td>\n",
       "      <td>-0.001730</td>\n",
       "      <td>0.000551</td>\n",
       "      <td>-0.000279</td>\n",
       "      <td>-0.000812</td>\n",
       "      <td>0.000762</td>\n",
       "      <td>-0.001037</td>\n",
       "      <td>-0.000392</td>\n",
       "      <td>-0.001551</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>-0.001577</td>\n",
       "      <td>0.000874</td>\n",
       "      <td>0.001451</td>\n",
       "      <td>0.000335</td>\n",
       "      <td>-0.000035</td>\n",
       "      <td>0.000658</td>\n",
       "      <td>-0.000397</td>\n",
       "      <td>-0.000025</td>\n",
       "      <td>-0.000606</td>\n",
       "      <td>0.001586</td>\n",
       "      <td>...</td>\n",
       "      <td>0.001408</td>\n",
       "      <td>-0.001667</td>\n",
       "      <td>0.000612</td>\n",
       "      <td>0.000680</td>\n",
       "      <td>-0.000541</td>\n",
       "      <td>-0.000034</td>\n",
       "      <td>-0.001041</td>\n",
       "      <td>0.001696</td>\n",
       "      <td>-0.000182</td>\n",
       "      <td>0.001794</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>0.000446</td>\n",
       "      <td>-0.000805</td>\n",
       "      <td>-0.000977</td>\n",
       "      <td>0.001022</td>\n",
       "      <td>0.001221</td>\n",
       "      <td>-0.000854</td>\n",
       "      <td>-0.000699</td>\n",
       "      <td>-0.000245</td>\n",
       "      <td>0.000551</td>\n",
       "      <td>0.000680</td>\n",
       "      <td>...</td>\n",
       "      <td>0.000832</td>\n",
       "      <td>0.000906</td>\n",
       "      <td>0.000805</td>\n",
       "      <td>-0.000685</td>\n",
       "      <td>0.000153</td>\n",
       "      <td>0.001087</td>\n",
       "      <td>0.000995</td>\n",
       "      <td>0.000566</td>\n",
       "      <td>0.001933</td>\n",
       "      <td>0.000481</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>0.000722</td>\n",
       "      <td>-0.002478</td>\n",
       "      <td>0.000407</td>\n",
       "      <td>0.000787</td>\n",
       "      <td>0.000632</td>\n",
       "      <td>-0.000190</td>\n",
       "      <td>-0.000070</td>\n",
       "      <td>0.000646</td>\n",
       "      <td>0.000886</td>\n",
       "      <td>0.000628</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.001350</td>\n",
       "      <td>0.000034</td>\n",
       "      <td>-0.001975</td>\n",
       "      <td>-0.001061</td>\n",
       "      <td>-0.000908</td>\n",
       "      <td>-0.001314</td>\n",
       "      <td>0.000865</td>\n",
       "      <td>-0.002277</td>\n",
       "      <td>0.000787</td>\n",
       "      <td>0.000646</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>0.000485</td>\n",
       "      <td>-0.000405</td>\n",
       "      <td>-0.000541</td>\n",
       "      <td>0.000058</td>\n",
       "      <td>-0.000583</td>\n",
       "      <td>-0.001148</td>\n",
       "      <td>-0.000786</td>\n",
       "      <td>-0.000176</td>\n",
       "      <td>0.000432</td>\n",
       "      <td>0.000472</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.000271</td>\n",
       "      <td>-0.000337</td>\n",
       "      <td>-0.000620</td>\n",
       "      <td>0.000522</td>\n",
       "      <td>-0.001104</td>\n",
       "      <td>-0.000536</td>\n",
       "      <td>0.000259</td>\n",
       "      <td>0.000451</td>\n",
       "      <td>-0.000170</td>\n",
       "      <td>-0.000473</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>0.000164</td>\n",
       "      <td>0.000122</td>\n",
       "      <td>-0.001479</td>\n",
       "      <td>-0.000377</td>\n",
       "      <td>0.001008</td>\n",
       "      <td>-0.001297</td>\n",
       "      <td>-0.001266</td>\n",
       "      <td>-0.000591</td>\n",
       "      <td>0.000162</td>\n",
       "      <td>-0.000152</td>\n",
       "      <td>...</td>\n",
       "      <td>0.001476</td>\n",
       "      <td>0.000463</td>\n",
       "      <td>-0.000987</td>\n",
       "      <td>-0.002597</td>\n",
       "      <td>0.000417</td>\n",
       "      <td>0.000417</td>\n",
       "      <td>-0.000809</td>\n",
       "      <td>0.000179</td>\n",
       "      <td>0.000993</td>\n",
       "      <td>0.001934</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>0.000023</td>\n",
       "      <td>0.000304</td>\n",
       "      <td>-0.001517</td>\n",
       "      <td>0.000507</td>\n",
       "      <td>-0.000595</td>\n",
       "      <td>0.001119</td>\n",
       "      <td>-0.000178</td>\n",
       "      <td>-0.000064</td>\n",
       "      <td>-0.002225</td>\n",
       "      <td>-0.000060</td>\n",
       "      <td>...</td>\n",
       "      <td>0.000693</td>\n",
       "      <td>0.000513</td>\n",
       "      <td>0.001406</td>\n",
       "      <td>0.000255</td>\n",
       "      <td>-0.000702</td>\n",
       "      <td>-0.000452</td>\n",
       "      <td>-0.000234</td>\n",
       "      <td>0.000847</td>\n",
       "      <td>0.000810</td>\n",
       "      <td>-0.000701</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>0.001082</td>\n",
       "      <td>0.000189</td>\n",
       "      <td>0.000467</td>\n",
       "      <td>0.001823</td>\n",
       "      <td>-0.001399</td>\n",
       "      <td>-0.000232</td>\n",
       "      <td>0.001759</td>\n",
       "      <td>0.000974</td>\n",
       "      <td>-0.000181</td>\n",
       "      <td>0.001010</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.000037</td>\n",
       "      <td>-0.002497</td>\n",
       "      <td>0.001697</td>\n",
       "      <td>-0.002078</td>\n",
       "      <td>-0.001068</td>\n",
       "      <td>-0.000022</td>\n",
       "      <td>-0.000466</td>\n",
       "      <td>0.000611</td>\n",
       "      <td>-0.000582</td>\n",
       "      <td>0.000507</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>-0.001103</td>\n",
       "      <td>-0.001062</td>\n",
       "      <td>0.000739</td>\n",
       "      <td>0.000631</td>\n",
       "      <td>-0.000484</td>\n",
       "      <td>-0.002179</td>\n",
       "      <td>0.000083</td>\n",
       "      <td>-0.000778</td>\n",
       "      <td>0.000039</td>\n",
       "      <td>0.000058</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.000033</td>\n",
       "      <td>0.000435</td>\n",
       "      <td>0.000677</td>\n",
       "      <td>0.001035</td>\n",
       "      <td>0.000619</td>\n",
       "      <td>0.001631</td>\n",
       "      <td>0.000985</td>\n",
       "      <td>-0.001332</td>\n",
       "      <td>0.000407</td>\n",
       "      <td>0.000752</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>0.000315</td>\n",
       "      <td>0.000899</td>\n",
       "      <td>0.001204</td>\n",
       "      <td>0.000423</td>\n",
       "      <td>-0.003230</td>\n",
       "      <td>-0.000157</td>\n",
       "      <td>0.000095</td>\n",
       "      <td>-0.000886</td>\n",
       "      <td>0.001157</td>\n",
       "      <td>-0.000135</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.001103</td>\n",
       "      <td>0.000281</td>\n",
       "      <td>-0.000793</td>\n",
       "      <td>0.001387</td>\n",
       "      <td>-0.000469</td>\n",
       "      <td>0.000344</td>\n",
       "      <td>-0.001699</td>\n",
       "      <td>-0.000254</td>\n",
       "      <td>-0.000033</td>\n",
       "      <td>0.000456</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>-0.001583</td>\n",
       "      <td>0.000931</td>\n",
       "      <td>0.001130</td>\n",
       "      <td>-0.001339</td>\n",
       "      <td>-0.000293</td>\n",
       "      <td>-0.000965</td>\n",
       "      <td>0.001113</td>\n",
       "      <td>0.000372</td>\n",
       "      <td>0.001269</td>\n",
       "      <td>0.000601</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.001262</td>\n",
       "      <td>-0.000393</td>\n",
       "      <td>0.000809</td>\n",
       "      <td>-0.000767</td>\n",
       "      <td>0.000090</td>\n",
       "      <td>0.000291</td>\n",
       "      <td>0.000338</td>\n",
       "      <td>-0.001634</td>\n",
       "      <td>-0.000213</td>\n",
       "      <td>0.000182</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>-0.001602</td>\n",
       "      <td>-0.000192</td>\n",
       "      <td>0.000517</td>\n",
       "      <td>-0.000180</td>\n",
       "      <td>-0.000602</td>\n",
       "      <td>-0.000177</td>\n",
       "      <td>-0.000918</td>\n",
       "      <td>0.001884</td>\n",
       "      <td>-0.000878</td>\n",
       "      <td>0.001399</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.000223</td>\n",
       "      <td>0.000759</td>\n",
       "      <td>-0.000121</td>\n",
       "      <td>0.000245</td>\n",
       "      <td>0.002565</td>\n",
       "      <td>-0.001115</td>\n",
       "      <td>-0.002090</td>\n",
       "      <td>-0.001452</td>\n",
       "      <td>-0.001086</td>\n",
       "      <td>0.000530</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>0.001042</td>\n",
       "      <td>0.000253</td>\n",
       "      <td>0.001152</td>\n",
       "      <td>-0.000690</td>\n",
       "      <td>-0.000039</td>\n",
       "      <td>0.000031</td>\n",
       "      <td>0.000956</td>\n",
       "      <td>-0.000755</td>\n",
       "      <td>-0.001259</td>\n",
       "      <td>-0.000121</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.000141</td>\n",
       "      <td>-0.001729</td>\n",
       "      <td>-0.000495</td>\n",
       "      <td>-0.000717</td>\n",
       "      <td>-0.000848</td>\n",
       "      <td>-0.000177</td>\n",
       "      <td>0.000265</td>\n",
       "      <td>0.000706</td>\n",
       "      <td>0.000157</td>\n",
       "      <td>0.000637</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>0.000140</td>\n",
       "      <td>-0.001849</td>\n",
       "      <td>-0.000290</td>\n",
       "      <td>-0.000805</td>\n",
       "      <td>-0.000487</td>\n",
       "      <td>-0.000455</td>\n",
       "      <td>-0.000830</td>\n",
       "      <td>0.001153</td>\n",
       "      <td>0.000347</td>\n",
       "      <td>0.000759</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.001215</td>\n",
       "      <td>0.000680</td>\n",
       "      <td>-0.001013</td>\n",
       "      <td>0.000917</td>\n",
       "      <td>0.000243</td>\n",
       "      <td>-0.000257</td>\n",
       "      <td>-0.000024</td>\n",
       "      <td>-0.000733</td>\n",
       "      <td>-0.000612</td>\n",
       "      <td>0.000203</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>0.000583</td>\n",
       "      <td>0.001511</td>\n",
       "      <td>0.001425</td>\n",
       "      <td>-0.000060</td>\n",
       "      <td>-0.001298</td>\n",
       "      <td>0.001010</td>\n",
       "      <td>-0.000839</td>\n",
       "      <td>0.001836</td>\n",
       "      <td>0.000557</td>\n",
       "      <td>0.000685</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.001387</td>\n",
       "      <td>-0.000584</td>\n",
       "      <td>-0.000195</td>\n",
       "      <td>0.000428</td>\n",
       "      <td>0.000396</td>\n",
       "      <td>0.000235</td>\n",
       "      <td>0.000694</td>\n",
       "      <td>-0.001528</td>\n",
       "      <td>-0.000856</td>\n",
       "      <td>0.000832</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>-0.000240</td>\n",
       "      <td>0.000155</td>\n",
       "      <td>-0.000388</td>\n",
       "      <td>-0.000121</td>\n",
       "      <td>0.000297</td>\n",
       "      <td>0.000416</td>\n",
       "      <td>-0.002219</td>\n",
       "      <td>0.000570</td>\n",
       "      <td>-0.000989</td>\n",
       "      <td>-0.001520</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.000491</td>\n",
       "      <td>0.000051</td>\n",
       "      <td>-0.000429</td>\n",
       "      <td>0.001595</td>\n",
       "      <td>-0.000653</td>\n",
       "      <td>0.002506</td>\n",
       "      <td>-0.000731</td>\n",
       "      <td>0.000153</td>\n",
       "      <td>0.000125</td>\n",
       "      <td>-0.001802</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>-0.000556</td>\n",
       "      <td>-0.000311</td>\n",
       "      <td>0.000724</td>\n",
       "      <td>-0.000046</td>\n",
       "      <td>0.000391</td>\n",
       "      <td>0.000177</td>\n",
       "      <td>-0.000379</td>\n",
       "      <td>-0.000212</td>\n",
       "      <td>-0.000291</td>\n",
       "      <td>-0.000415</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.000993</td>\n",
       "      <td>0.000225</td>\n",
       "      <td>0.000565</td>\n",
       "      <td>0.001584</td>\n",
       "      <td>-0.001211</td>\n",
       "      <td>-0.000253</td>\n",
       "      <td>-0.000312</td>\n",
       "      <td>-0.000581</td>\n",
       "      <td>0.000609</td>\n",
       "      <td>0.000195</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>30</th>\n",
       "      <td>0.000779</td>\n",
       "      <td>0.000716</td>\n",
       "      <td>-0.000918</td>\n",
       "      <td>0.001095</td>\n",
       "      <td>-0.000597</td>\n",
       "      <td>0.000536</td>\n",
       "      <td>0.000224</td>\n",
       "      <td>-0.001125</td>\n",
       "      <td>-0.000384</td>\n",
       "      <td>-0.001503</td>\n",
       "      <td>...</td>\n",
       "      <td>0.000234</td>\n",
       "      <td>-0.000708</td>\n",
       "      <td>0.001656</td>\n",
       "      <td>0.000808</td>\n",
       "      <td>-0.001123</td>\n",
       "      <td>-0.000306</td>\n",
       "      <td>-0.000002</td>\n",
       "      <td>0.001191</td>\n",
       "      <td>-0.000423</td>\n",
       "      <td>-0.001852</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31</th>\n",
       "      <td>0.001856</td>\n",
       "      <td>0.001688</td>\n",
       "      <td>-0.000984</td>\n",
       "      <td>0.001468</td>\n",
       "      <td>0.000611</td>\n",
       "      <td>0.001426</td>\n",
       "      <td>0.000167</td>\n",
       "      <td>-0.000446</td>\n",
       "      <td>-0.001268</td>\n",
       "      <td>0.001406</td>\n",
       "      <td>...</td>\n",
       "      <td>0.000294</td>\n",
       "      <td>0.000118</td>\n",
       "      <td>-0.001144</td>\n",
       "      <td>0.000035</td>\n",
       "      <td>0.000752</td>\n",
       "      <td>-0.000125</td>\n",
       "      <td>0.000117</td>\n",
       "      <td>-0.001144</td>\n",
       "      <td>-0.000149</td>\n",
       "      <td>-0.000108</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>32</th>\n",
       "      <td>0.002585</td>\n",
       "      <td>-0.001678</td>\n",
       "      <td>-0.000176</td>\n",
       "      <td>0.000997</td>\n",
       "      <td>-0.000563</td>\n",
       "      <td>0.001345</td>\n",
       "      <td>0.001914</td>\n",
       "      <td>-0.001833</td>\n",
       "      <td>-0.000501</td>\n",
       "      <td>-0.000794</td>\n",
       "      <td>...</td>\n",
       "      <td>0.001784</td>\n",
       "      <td>0.001240</td>\n",
       "      <td>-0.001892</td>\n",
       "      <td>-0.000300</td>\n",
       "      <td>-0.000348</td>\n",
       "      <td>0.000121</td>\n",
       "      <td>0.001918</td>\n",
       "      <td>-0.000129</td>\n",
       "      <td>-0.000142</td>\n",
       "      <td>-0.000616</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>33</th>\n",
       "      <td>0.000308</td>\n",
       "      <td>-0.001621</td>\n",
       "      <td>-0.001256</td>\n",
       "      <td>-0.000639</td>\n",
       "      <td>0.001003</td>\n",
       "      <td>0.001688</td>\n",
       "      <td>0.001564</td>\n",
       "      <td>-0.000546</td>\n",
       "      <td>0.000994</td>\n",
       "      <td>0.000753</td>\n",
       "      <td>...</td>\n",
       "      <td>0.000764</td>\n",
       "      <td>-0.000552</td>\n",
       "      <td>-0.001384</td>\n",
       "      <td>0.000692</td>\n",
       "      <td>0.000655</td>\n",
       "      <td>-0.000659</td>\n",
       "      <td>0.000853</td>\n",
       "      <td>-0.001151</td>\n",
       "      <td>0.000616</td>\n",
       "      <td>-0.002950</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>34</th>\n",
       "      <td>0.000429</td>\n",
       "      <td>-0.000670</td>\n",
       "      <td>0.000440</td>\n",
       "      <td>-0.002985</td>\n",
       "      <td>0.000584</td>\n",
       "      <td>0.000163</td>\n",
       "      <td>-0.001539</td>\n",
       "      <td>0.000508</td>\n",
       "      <td>-0.001629</td>\n",
       "      <td>0.000559</td>\n",
       "      <td>...</td>\n",
       "      <td>0.000800</td>\n",
       "      <td>0.000295</td>\n",
       "      <td>0.000642</td>\n",
       "      <td>0.000903</td>\n",
       "      <td>-0.000765</td>\n",
       "      <td>0.000035</td>\n",
       "      <td>-0.001070</td>\n",
       "      <td>0.000722</td>\n",
       "      <td>-0.000235</td>\n",
       "      <td>-0.000357</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>35</th>\n",
       "      <td>-0.000640</td>\n",
       "      <td>0.000185</td>\n",
       "      <td>-0.000109</td>\n",
       "      <td>0.000023</td>\n",
       "      <td>0.000681</td>\n",
       "      <td>-0.000210</td>\n",
       "      <td>-0.000715</td>\n",
       "      <td>-0.000816</td>\n",
       "      <td>-0.000088</td>\n",
       "      <td>-0.000475</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.000804</td>\n",
       "      <td>-0.000657</td>\n",
       "      <td>0.002716</td>\n",
       "      <td>-0.001623</td>\n",
       "      <td>-0.001134</td>\n",
       "      <td>0.000335</td>\n",
       "      <td>0.001080</td>\n",
       "      <td>-0.000546</td>\n",
       "      <td>-0.000098</td>\n",
       "      <td>0.001442</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>36</th>\n",
       "      <td>-0.000639</td>\n",
       "      <td>-0.000449</td>\n",
       "      <td>-0.000153</td>\n",
       "      <td>0.000064</td>\n",
       "      <td>0.000116</td>\n",
       "      <td>0.000696</td>\n",
       "      <td>-0.000706</td>\n",
       "      <td>-0.001391</td>\n",
       "      <td>0.000570</td>\n",
       "      <td>0.000149</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.001626</td>\n",
       "      <td>-0.000507</td>\n",
       "      <td>0.001653</td>\n",
       "      <td>-0.001244</td>\n",
       "      <td>0.001091</td>\n",
       "      <td>-0.000758</td>\n",
       "      <td>-0.000310</td>\n",
       "      <td>0.000739</td>\n",
       "      <td>0.000624</td>\n",
       "      <td>0.002449</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>37</th>\n",
       "      <td>-0.000205</td>\n",
       "      <td>-0.000666</td>\n",
       "      <td>-0.000649</td>\n",
       "      <td>0.000113</td>\n",
       "      <td>0.000238</td>\n",
       "      <td>-0.000775</td>\n",
       "      <td>-0.001532</td>\n",
       "      <td>0.000471</td>\n",
       "      <td>0.000150</td>\n",
       "      <td>0.001513</td>\n",
       "      <td>...</td>\n",
       "      <td>0.000498</td>\n",
       "      <td>0.001410</td>\n",
       "      <td>-0.000937</td>\n",
       "      <td>0.001380</td>\n",
       "      <td>0.000514</td>\n",
       "      <td>0.001698</td>\n",
       "      <td>-0.001318</td>\n",
       "      <td>0.001638</td>\n",
       "      <td>-0.001391</td>\n",
       "      <td>-0.001230</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>38</th>\n",
       "      <td>-0.001557</td>\n",
       "      <td>0.000745</td>\n",
       "      <td>0.000406</td>\n",
       "      <td>0.000644</td>\n",
       "      <td>-0.002045</td>\n",
       "      <td>-0.000298</td>\n",
       "      <td>0.000562</td>\n",
       "      <td>0.000224</td>\n",
       "      <td>0.000529</td>\n",
       "      <td>-0.001762</td>\n",
       "      <td>...</td>\n",
       "      <td>0.001556</td>\n",
       "      <td>0.000715</td>\n",
       "      <td>0.000370</td>\n",
       "      <td>-0.000110</td>\n",
       "      <td>-0.000074</td>\n",
       "      <td>0.000368</td>\n",
       "      <td>-0.000721</td>\n",
       "      <td>0.000044</td>\n",
       "      <td>-0.000305</td>\n",
       "      <td>0.000866</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>39</th>\n",
       "      <td>0.000182</td>\n",
       "      <td>0.000702</td>\n",
       "      <td>0.000628</td>\n",
       "      <td>-0.000193</td>\n",
       "      <td>0.001504</td>\n",
       "      <td>-0.000379</td>\n",
       "      <td>-0.000816</td>\n",
       "      <td>-0.000363</td>\n",
       "      <td>0.000218</td>\n",
       "      <td>-0.000567</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.000395</td>\n",
       "      <td>-0.001320</td>\n",
       "      <td>0.000852</td>\n",
       "      <td>-0.000839</td>\n",
       "      <td>0.000249</td>\n",
       "      <td>-0.001526</td>\n",
       "      <td>-0.002097</td>\n",
       "      <td>0.000067</td>\n",
       "      <td>0.001506</td>\n",
       "      <td>0.000687</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>40</th>\n",
       "      <td>0.001056</td>\n",
       "      <td>0.001190</td>\n",
       "      <td>0.001462</td>\n",
       "      <td>-0.000313</td>\n",
       "      <td>-0.001290</td>\n",
       "      <td>0.000593</td>\n",
       "      <td>0.000873</td>\n",
       "      <td>-0.000270</td>\n",
       "      <td>0.000033</td>\n",
       "      <td>0.000049</td>\n",
       "      <td>...</td>\n",
       "      <td>0.000305</td>\n",
       "      <td>0.000150</td>\n",
       "      <td>0.000398</td>\n",
       "      <td>0.000085</td>\n",
       "      <td>0.000217</td>\n",
       "      <td>-0.000041</td>\n",
       "      <td>0.000004</td>\n",
       "      <td>0.000175</td>\n",
       "      <td>0.001967</td>\n",
       "      <td>0.000187</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>41</th>\n",
       "      <td>-0.000702</td>\n",
       "      <td>-0.000450</td>\n",
       "      <td>-0.000229</td>\n",
       "      <td>0.000461</td>\n",
       "      <td>0.001554</td>\n",
       "      <td>0.000726</td>\n",
       "      <td>-0.000061</td>\n",
       "      <td>0.000042</td>\n",
       "      <td>0.000279</td>\n",
       "      <td>-0.000769</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.000750</td>\n",
       "      <td>-0.000137</td>\n",
       "      <td>-0.001061</td>\n",
       "      <td>-0.002145</td>\n",
       "      <td>0.000165</td>\n",
       "      <td>0.000937</td>\n",
       "      <td>0.002029</td>\n",
       "      <td>-0.000602</td>\n",
       "      <td>-0.000758</td>\n",
       "      <td>-0.000545</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>42</th>\n",
       "      <td>-0.002091</td>\n",
       "      <td>-0.001651</td>\n",
       "      <td>-0.000672</td>\n",
       "      <td>-0.001487</td>\n",
       "      <td>-0.001156</td>\n",
       "      <td>-0.000739</td>\n",
       "      <td>0.001193</td>\n",
       "      <td>0.000199</td>\n",
       "      <td>0.000988</td>\n",
       "      <td>0.000152</td>\n",
       "      <td>...</td>\n",
       "      <td>0.000080</td>\n",
       "      <td>0.000605</td>\n",
       "      <td>-0.002424</td>\n",
       "      <td>0.000133</td>\n",
       "      <td>0.000787</td>\n",
       "      <td>-0.000841</td>\n",
       "      <td>-0.000219</td>\n",
       "      <td>0.000525</td>\n",
       "      <td>-0.002364</td>\n",
       "      <td>-0.000008</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>43</th>\n",
       "      <td>-0.000998</td>\n",
       "      <td>0.000658</td>\n",
       "      <td>0.000907</td>\n",
       "      <td>0.000005</td>\n",
       "      <td>0.001216</td>\n",
       "      <td>-0.000910</td>\n",
       "      <td>0.001421</td>\n",
       "      <td>0.000504</td>\n",
       "      <td>0.000628</td>\n",
       "      <td>0.000021</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.000250</td>\n",
       "      <td>-0.000044</td>\n",
       "      <td>0.000949</td>\n",
       "      <td>0.000839</td>\n",
       "      <td>0.000496</td>\n",
       "      <td>-0.000713</td>\n",
       "      <td>0.001580</td>\n",
       "      <td>-0.000357</td>\n",
       "      <td>-0.000896</td>\n",
       "      <td>0.000272</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>44</th>\n",
       "      <td>-0.000747</td>\n",
       "      <td>0.000459</td>\n",
       "      <td>0.000775</td>\n",
       "      <td>0.000513</td>\n",
       "      <td>0.000243</td>\n",
       "      <td>0.000324</td>\n",
       "      <td>0.000130</td>\n",
       "      <td>0.000140</td>\n",
       "      <td>0.000454</td>\n",
       "      <td>0.001348</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.000671</td>\n",
       "      <td>0.001210</td>\n",
       "      <td>-0.000176</td>\n",
       "      <td>0.000243</td>\n",
       "      <td>0.000523</td>\n",
       "      <td>0.000567</td>\n",
       "      <td>-0.000892</td>\n",
       "      <td>-0.000829</td>\n",
       "      <td>0.000702</td>\n",
       "      <td>0.000534</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>45</th>\n",
       "      <td>-0.000959</td>\n",
       "      <td>0.000549</td>\n",
       "      <td>0.000810</td>\n",
       "      <td>-0.000622</td>\n",
       "      <td>-0.000656</td>\n",
       "      <td>-0.000544</td>\n",
       "      <td>0.000253</td>\n",
       "      <td>0.000576</td>\n",
       "      <td>0.001502</td>\n",
       "      <td>-0.001496</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.000600</td>\n",
       "      <td>0.000810</td>\n",
       "      <td>0.002511</td>\n",
       "      <td>-0.001177</td>\n",
       "      <td>0.001047</td>\n",
       "      <td>0.000613</td>\n",
       "      <td>0.001361</td>\n",
       "      <td>0.001620</td>\n",
       "      <td>-0.000765</td>\n",
       "      <td>-0.000239</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>46</th>\n",
       "      <td>0.000683</td>\n",
       "      <td>-0.000348</td>\n",
       "      <td>0.001010</td>\n",
       "      <td>0.000735</td>\n",
       "      <td>-0.001136</td>\n",
       "      <td>-0.000678</td>\n",
       "      <td>-0.000021</td>\n",
       "      <td>-0.000473</td>\n",
       "      <td>-0.000035</td>\n",
       "      <td>0.000927</td>\n",
       "      <td>...</td>\n",
       "      <td>0.001583</td>\n",
       "      <td>0.000156</td>\n",
       "      <td>-0.000510</td>\n",
       "      <td>0.000631</td>\n",
       "      <td>-0.002067</td>\n",
       "      <td>-0.000622</td>\n",
       "      <td>-0.000453</td>\n",
       "      <td>-0.000649</td>\n",
       "      <td>0.001802</td>\n",
       "      <td>-0.000773</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>47</th>\n",
       "      <td>-0.000158</td>\n",
       "      <td>0.000128</td>\n",
       "      <td>0.000127</td>\n",
       "      <td>0.000355</td>\n",
       "      <td>0.000165</td>\n",
       "      <td>0.001527</td>\n",
       "      <td>-0.000134</td>\n",
       "      <td>0.000316</td>\n",
       "      <td>0.000865</td>\n",
       "      <td>0.000130</td>\n",
       "      <td>...</td>\n",
       "      <td>0.000802</td>\n",
       "      <td>0.001375</td>\n",
       "      <td>0.000223</td>\n",
       "      <td>-0.000519</td>\n",
       "      <td>-0.000704</td>\n",
       "      <td>0.000051</td>\n",
       "      <td>-0.000099</td>\n",
       "      <td>0.000454</td>\n",
       "      <td>-0.001211</td>\n",
       "      <td>-0.001829</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>48</th>\n",
       "      <td>0.000444</td>\n",
       "      <td>-0.001874</td>\n",
       "      <td>0.000123</td>\n",
       "      <td>0.000564</td>\n",
       "      <td>-0.000823</td>\n",
       "      <td>-0.000421</td>\n",
       "      <td>-0.002387</td>\n",
       "      <td>0.000235</td>\n",
       "      <td>0.001043</td>\n",
       "      <td>0.000218</td>\n",
       "      <td>...</td>\n",
       "      <td>0.000381</td>\n",
       "      <td>-0.000830</td>\n",
       "      <td>0.000397</td>\n",
       "      <td>-0.002000</td>\n",
       "      <td>0.000721</td>\n",
       "      <td>0.001275</td>\n",
       "      <td>0.001348</td>\n",
       "      <td>-0.000214</td>\n",
       "      <td>0.001253</td>\n",
       "      <td>0.000533</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>49</th>\n",
       "      <td>-0.001384</td>\n",
       "      <td>0.001251</td>\n",
       "      <td>0.000351</td>\n",
       "      <td>0.000857</td>\n",
       "      <td>0.000633</td>\n",
       "      <td>-0.000472</td>\n",
       "      <td>0.000165</td>\n",
       "      <td>-0.000712</td>\n",
       "      <td>0.000424</td>\n",
       "      <td>0.000755</td>\n",
       "      <td>...</td>\n",
       "      <td>0.001098</td>\n",
       "      <td>0.000106</td>\n",
       "      <td>-0.001084</td>\n",
       "      <td>-0.000454</td>\n",
       "      <td>0.001707</td>\n",
       "      <td>0.000354</td>\n",
       "      <td>-0.000744</td>\n",
       "      <td>0.001836</td>\n",
       "      <td>0.001423</td>\n",
       "      <td>0.001561</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>50 rows × 300 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "         0         1         2         3         4         5         6    \\\n",
       "0  -0.001167 -0.000852 -0.000492 -0.000795 -0.001282 -0.000203 -0.000213   \n",
       "1  -0.001839  0.000358 -0.000525 -0.000972  0.000426 -0.000366 -0.000932   \n",
       "2   0.001617  0.000505 -0.000844  0.000573  0.000859 -0.001510 -0.000200   \n",
       "3  -0.001965 -0.000314 -0.000015 -0.002073  0.001060 -0.000386  0.001254   \n",
       "4  -0.000329  0.000325  0.000001 -0.000809 -0.000736 -0.000781  0.000654   \n",
       "5   0.000937  0.000211 -0.000725 -0.000073  0.001798  0.000430  0.000324   \n",
       "6   0.001466  0.000414 -0.000467  0.000546 -0.000096 -0.000146 -0.001521   \n",
       "7   0.000531  0.000220 -0.001428 -0.000380 -0.000647  0.000736  0.001520   \n",
       "8  -0.001787  0.001369 -0.001070  0.000125 -0.000637  0.000097  0.000691   \n",
       "9   0.000888 -0.000658 -0.000056 -0.000202  0.000617 -0.000344  0.000730   \n",
       "10 -0.000045  0.000936 -0.001420 -0.000151 -0.000109  0.000317  0.002315   \n",
       "11 -0.001345  0.000046  0.001301  0.001642 -0.000205 -0.000573 -0.000580   \n",
       "12  0.000168  0.000572  0.000344 -0.000703  0.001349  0.001633 -0.000863   \n",
       "13  0.000254  0.000065 -0.000256 -0.000456  0.000140  0.001723  0.000759   \n",
       "14 -0.001577  0.000874  0.001451  0.000335 -0.000035  0.000658 -0.000397   \n",
       "15  0.000446 -0.000805 -0.000977  0.001022  0.001221 -0.000854 -0.000699   \n",
       "16  0.000722 -0.002478  0.000407  0.000787  0.000632 -0.000190 -0.000070   \n",
       "17  0.000485 -0.000405 -0.000541  0.000058 -0.000583 -0.001148 -0.000786   \n",
       "18  0.000164  0.000122 -0.001479 -0.000377  0.001008 -0.001297 -0.001266   \n",
       "19  0.000023  0.000304 -0.001517  0.000507 -0.000595  0.001119 -0.000178   \n",
       "20  0.001082  0.000189  0.000467  0.001823 -0.001399 -0.000232  0.001759   \n",
       "21 -0.001103 -0.001062  0.000739  0.000631 -0.000484 -0.002179  0.000083   \n",
       "22  0.000315  0.000899  0.001204  0.000423 -0.003230 -0.000157  0.000095   \n",
       "23 -0.001583  0.000931  0.001130 -0.001339 -0.000293 -0.000965  0.001113   \n",
       "24 -0.001602 -0.000192  0.000517 -0.000180 -0.000602 -0.000177 -0.000918   \n",
       "25  0.001042  0.000253  0.001152 -0.000690 -0.000039  0.000031  0.000956   \n",
       "26  0.000140 -0.001849 -0.000290 -0.000805 -0.000487 -0.000455 -0.000830   \n",
       "27  0.000583  0.001511  0.001425 -0.000060 -0.001298  0.001010 -0.000839   \n",
       "28 -0.000240  0.000155 -0.000388 -0.000121  0.000297  0.000416 -0.002219   \n",
       "29 -0.000556 -0.000311  0.000724 -0.000046  0.000391  0.000177 -0.000379   \n",
       "30  0.000779  0.000716 -0.000918  0.001095 -0.000597  0.000536  0.000224   \n",
       "31  0.001856  0.001688 -0.000984  0.001468  0.000611  0.001426  0.000167   \n",
       "32  0.002585 -0.001678 -0.000176  0.000997 -0.000563  0.001345  0.001914   \n",
       "33  0.000308 -0.001621 -0.001256 -0.000639  0.001003  0.001688  0.001564   \n",
       "34  0.000429 -0.000670  0.000440 -0.002985  0.000584  0.000163 -0.001539   \n",
       "35 -0.000640  0.000185 -0.000109  0.000023  0.000681 -0.000210 -0.000715   \n",
       "36 -0.000639 -0.000449 -0.000153  0.000064  0.000116  0.000696 -0.000706   \n",
       "37 -0.000205 -0.000666 -0.000649  0.000113  0.000238 -0.000775 -0.001532   \n",
       "38 -0.001557  0.000745  0.000406  0.000644 -0.002045 -0.000298  0.000562   \n",
       "39  0.000182  0.000702  0.000628 -0.000193  0.001504 -0.000379 -0.000816   \n",
       "40  0.001056  0.001190  0.001462 -0.000313 -0.001290  0.000593  0.000873   \n",
       "41 -0.000702 -0.000450 -0.000229  0.000461  0.001554  0.000726 -0.000061   \n",
       "42 -0.002091 -0.001651 -0.000672 -0.001487 -0.001156 -0.000739  0.001193   \n",
       "43 -0.000998  0.000658  0.000907  0.000005  0.001216 -0.000910  0.001421   \n",
       "44 -0.000747  0.000459  0.000775  0.000513  0.000243  0.000324  0.000130   \n",
       "45 -0.000959  0.000549  0.000810 -0.000622 -0.000656 -0.000544  0.000253   \n",
       "46  0.000683 -0.000348  0.001010  0.000735 -0.001136 -0.000678 -0.000021   \n",
       "47 -0.000158  0.000128  0.000127  0.000355  0.000165  0.001527 -0.000134   \n",
       "48  0.000444 -0.001874  0.000123  0.000564 -0.000823 -0.000421 -0.002387   \n",
       "49 -0.001384  0.001251  0.000351  0.000857  0.000633 -0.000472  0.000165   \n",
       "\n",
       "         7         8         9      ...          290       291       292  \\\n",
       "0  -0.000801 -0.000645  0.000184    ...    -0.000085  0.000900 -0.000474   \n",
       "1  -0.000329 -0.000403  0.001878    ...     0.001952 -0.001530 -0.000582   \n",
       "2  -0.000161 -0.000136 -0.000521    ...    -0.000393 -0.001900 -0.000458   \n",
       "3   0.001901 -0.000897 -0.000069    ...     0.000017 -0.000286 -0.000736   \n",
       "4   0.001530  0.000205 -0.001387    ...    -0.000206 -0.000706  0.001770   \n",
       "5  -0.002008 -0.000065  0.000197    ...    -0.000548 -0.000935  0.000997   \n",
       "6   0.001587 -0.001638 -0.000982    ...     0.001106 -0.000414  0.000603   \n",
       "7  -0.000819 -0.000967  0.000404    ...    -0.000019  0.000467 -0.000768   \n",
       "8  -0.001382 -0.000948  0.000156    ...    -0.000495  0.000148  0.000771   \n",
       "9  -0.000287  0.000702 -0.001642    ...    -0.000976  0.001061 -0.000683   \n",
       "10 -0.000189  0.001648 -0.002117    ...    -0.000218  0.000849 -0.001876   \n",
       "11  0.002273 -0.000138 -0.001139    ...    -0.001405 -0.001263  0.000004   \n",
       "12 -0.000350 -0.000221  0.000981    ...     0.000253  0.001538  0.000803   \n",
       "13  0.001423 -0.000183 -0.001370    ...    -0.000357  0.000292 -0.001730   \n",
       "14 -0.000025 -0.000606  0.001586    ...     0.001408 -0.001667  0.000612   \n",
       "15 -0.000245  0.000551  0.000680    ...     0.000832  0.000906  0.000805   \n",
       "16  0.000646  0.000886  0.000628    ...    -0.001350  0.000034 -0.001975   \n",
       "17 -0.000176  0.000432  0.000472    ...    -0.000271 -0.000337 -0.000620   \n",
       "18 -0.000591  0.000162 -0.000152    ...     0.001476  0.000463 -0.000987   \n",
       "19 -0.000064 -0.002225 -0.000060    ...     0.000693  0.000513  0.001406   \n",
       "20  0.000974 -0.000181  0.001010    ...    -0.000037 -0.002497  0.001697   \n",
       "21 -0.000778  0.000039  0.000058    ...    -0.000033  0.000435  0.000677   \n",
       "22 -0.000886  0.001157 -0.000135    ...    -0.001103  0.000281 -0.000793   \n",
       "23  0.000372  0.001269  0.000601    ...    -0.001262 -0.000393  0.000809   \n",
       "24  0.001884 -0.000878  0.001399    ...    -0.000223  0.000759 -0.000121   \n",
       "25 -0.000755 -0.001259 -0.000121    ...    -0.000141 -0.001729 -0.000495   \n",
       "26  0.001153  0.000347  0.000759    ...    -0.001215  0.000680 -0.001013   \n",
       "27  0.001836  0.000557  0.000685    ...    -0.001387 -0.000584 -0.000195   \n",
       "28  0.000570 -0.000989 -0.001520    ...    -0.000491  0.000051 -0.000429   \n",
       "29 -0.000212 -0.000291 -0.000415    ...    -0.000993  0.000225  0.000565   \n",
       "30 -0.001125 -0.000384 -0.001503    ...     0.000234 -0.000708  0.001656   \n",
       "31 -0.000446 -0.001268  0.001406    ...     0.000294  0.000118 -0.001144   \n",
       "32 -0.001833 -0.000501 -0.000794    ...     0.001784  0.001240 -0.001892   \n",
       "33 -0.000546  0.000994  0.000753    ...     0.000764 -0.000552 -0.001384   \n",
       "34  0.000508 -0.001629  0.000559    ...     0.000800  0.000295  0.000642   \n",
       "35 -0.000816 -0.000088 -0.000475    ...    -0.000804 -0.000657  0.002716   \n",
       "36 -0.001391  0.000570  0.000149    ...    -0.001626 -0.000507  0.001653   \n",
       "37  0.000471  0.000150  0.001513    ...     0.000498  0.001410 -0.000937   \n",
       "38  0.000224  0.000529 -0.001762    ...     0.001556  0.000715  0.000370   \n",
       "39 -0.000363  0.000218 -0.000567    ...    -0.000395 -0.001320  0.000852   \n",
       "40 -0.000270  0.000033  0.000049    ...     0.000305  0.000150  0.000398   \n",
       "41  0.000042  0.000279 -0.000769    ...    -0.000750 -0.000137 -0.001061   \n",
       "42  0.000199  0.000988  0.000152    ...     0.000080  0.000605 -0.002424   \n",
       "43  0.000504  0.000628  0.000021    ...    -0.000250 -0.000044  0.000949   \n",
       "44  0.000140  0.000454  0.001348    ...    -0.000671  0.001210 -0.000176   \n",
       "45  0.000576  0.001502 -0.001496    ...    -0.000600  0.000810  0.002511   \n",
       "46 -0.000473 -0.000035  0.000927    ...     0.001583  0.000156 -0.000510   \n",
       "47  0.000316  0.000865  0.000130    ...     0.000802  0.001375  0.000223   \n",
       "48  0.000235  0.001043  0.000218    ...     0.000381 -0.000830  0.000397   \n",
       "49 -0.000712  0.000424  0.000755    ...     0.001098  0.000106 -0.001084   \n",
       "\n",
       "         293       294       295       296       297       298       299  \n",
       "0  -0.002101  0.000689  0.001434 -0.000477 -0.000222  0.000327  0.000888  \n",
       "1  -0.001114 -0.000631  0.000136 -0.000608  0.001984  0.000999  0.001334  \n",
       "2   0.000940  0.000022 -0.000768 -0.000272 -0.001530 -0.000510  0.000766  \n",
       "3   0.000873 -0.000074  0.001166  0.000495  0.000707 -0.000756 -0.002901  \n",
       "4   0.001336 -0.002049 -0.000436 -0.000529  0.000222  0.000522 -0.001656  \n",
       "5  -0.001046  0.000562 -0.000464 -0.000412  0.000817 -0.000229 -0.000322  \n",
       "6  -0.000837 -0.000255 -0.000541  0.000804 -0.001421 -0.001140 -0.000575  \n",
       "7   0.000021  0.000491 -0.003098 -0.000444  0.002383 -0.001050 -0.000303  \n",
       "8   0.000217 -0.000152 -0.001335  0.001246  0.000815 -0.000191  0.000051  \n",
       "9   0.000801  0.001206 -0.000838  0.000649  0.000011 -0.001107  0.001762  \n",
       "10  0.000900 -0.000092  0.000237 -0.001062  0.000570 -0.000171 -0.000584  \n",
       "11  0.001323  0.002033  0.000830  0.001080 -0.001044 -0.000881 -0.000020  \n",
       "12  0.001599 -0.001355  0.000827 -0.000951 -0.000623  0.000734 -0.000667  \n",
       "13  0.000551 -0.000279 -0.000812  0.000762 -0.001037 -0.000392 -0.001551  \n",
       "14  0.000680 -0.000541 -0.000034 -0.001041  0.001696 -0.000182  0.001794  \n",
       "15 -0.000685  0.000153  0.001087  0.000995  0.000566  0.001933  0.000481  \n",
       "16 -0.001061 -0.000908 -0.001314  0.000865 -0.002277  0.000787  0.000646  \n",
       "17  0.000522 -0.001104 -0.000536  0.000259  0.000451 -0.000170 -0.000473  \n",
       "18 -0.002597  0.000417  0.000417 -0.000809  0.000179  0.000993  0.001934  \n",
       "19  0.000255 -0.000702 -0.000452 -0.000234  0.000847  0.000810 -0.000701  \n",
       "20 -0.002078 -0.001068 -0.000022 -0.000466  0.000611 -0.000582  0.000507  \n",
       "21  0.001035  0.000619  0.001631  0.000985 -0.001332  0.000407  0.000752  \n",
       "22  0.001387 -0.000469  0.000344 -0.001699 -0.000254 -0.000033  0.000456  \n",
       "23 -0.000767  0.000090  0.000291  0.000338 -0.001634 -0.000213  0.000182  \n",
       "24  0.000245  0.002565 -0.001115 -0.002090 -0.001452 -0.001086  0.000530  \n",
       "25 -0.000717 -0.000848 -0.000177  0.000265  0.000706  0.000157  0.000637  \n",
       "26  0.000917  0.000243 -0.000257 -0.000024 -0.000733 -0.000612  0.000203  \n",
       "27  0.000428  0.000396  0.000235  0.000694 -0.001528 -0.000856  0.000832  \n",
       "28  0.001595 -0.000653  0.002506 -0.000731  0.000153  0.000125 -0.001802  \n",
       "29  0.001584 -0.001211 -0.000253 -0.000312 -0.000581  0.000609  0.000195  \n",
       "30  0.000808 -0.001123 -0.000306 -0.000002  0.001191 -0.000423 -0.001852  \n",
       "31  0.000035  0.000752 -0.000125  0.000117 -0.001144 -0.000149 -0.000108  \n",
       "32 -0.000300 -0.000348  0.000121  0.001918 -0.000129 -0.000142 -0.000616  \n",
       "33  0.000692  0.000655 -0.000659  0.000853 -0.001151  0.000616 -0.002950  \n",
       "34  0.000903 -0.000765  0.000035 -0.001070  0.000722 -0.000235 -0.000357  \n",
       "35 -0.001623 -0.001134  0.000335  0.001080 -0.000546 -0.000098  0.001442  \n",
       "36 -0.001244  0.001091 -0.000758 -0.000310  0.000739  0.000624  0.002449  \n",
       "37  0.001380  0.000514  0.001698 -0.001318  0.001638 -0.001391 -0.001230  \n",
       "38 -0.000110 -0.000074  0.000368 -0.000721  0.000044 -0.000305  0.000866  \n",
       "39 -0.000839  0.000249 -0.001526 -0.002097  0.000067  0.001506  0.000687  \n",
       "40  0.000085  0.000217 -0.000041  0.000004  0.000175  0.001967  0.000187  \n",
       "41 -0.002145  0.000165  0.000937  0.002029 -0.000602 -0.000758 -0.000545  \n",
       "42  0.000133  0.000787 -0.000841 -0.000219  0.000525 -0.002364 -0.000008  \n",
       "43  0.000839  0.000496 -0.000713  0.001580 -0.000357 -0.000896  0.000272  \n",
       "44  0.000243  0.000523  0.000567 -0.000892 -0.000829  0.000702  0.000534  \n",
       "45 -0.001177  0.001047  0.000613  0.001361  0.001620 -0.000765 -0.000239  \n",
       "46  0.000631 -0.002067 -0.000622 -0.000453 -0.000649  0.001802 -0.000773  \n",
       "47 -0.000519 -0.000704  0.000051 -0.000099  0.000454 -0.001211 -0.001829  \n",
       "48 -0.002000  0.000721  0.001275  0.001348 -0.000214  0.001253  0.000533  \n",
       "49 -0.000454  0.001707  0.000354 -0.000744  0.001836  0.001423  0.001561  \n",
       "\n",
       "[50 rows x 300 columns]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'''\n",
    "estimate specific return (residual return: R=XF+U)\n",
    "'''\n",
    "\n",
    "df_spec_ret=pd.DataFrame(spec_ret)\n",
    "df_spec_ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "corr_raw_np=corr_raw.values.reshape([N_T,N_features,N_features])\n",
    "sigma_raw_np=sigma_raw.values\n",
    "ans=np.dot(corr_raw_np,sigma_raw_np.reshape([N_T,N_features,1]))\n",
    "ans=corr_raw_np*sigma_raw_np.reshape([N_T,N_features,1])*sigma_raw_np.reshape([N_T,1,N_features])\n",
    "robust_cov=ans\n",
    "#cov=corr_mv_window.cov().values.reshape([N_T,N_features,N_features])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "'''\n",
    "compute XFX^T for each time period.\n",
    "\n",
    "we apply einstein summation on X_{tnk} and F_{tkk}\n",
    "'''\n",
    "tmp1=np.einsum('tnk,tkk->tnk', X, robust_cov)\n",
    "'''\n",
    "Here l=n.\n",
    "'''\n",
    "xfx=np.einsum('tnk,tkl->tnl', X, np.transpose(tmp1,(0,2,1)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(300, 5, 5)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "robust_cov.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1.25452330e-03, -2.19613328e-04, -3.31781153e-04,\n",
       "        -4.01260509e-04,  1.23317125e-05],\n",
       "       [-2.19613328e-04,  5.40761926e-04,  1.64023146e-05,\n",
       "        -1.31955747e-04, -3.13822953e-05],\n",
       "       [-3.31781153e-04,  1.64023146e-05,  6.32020945e-04,\n",
       "        -2.62930034e-04, -2.29428145e-05],\n",
       "       [-4.01260509e-04, -1.31955747e-04, -2.62930034e-04,\n",
       "         1.15966737e-03, -4.50962546e-04],\n",
       "       [ 1.23317125e-05, -3.13822953e-05, -2.29428145e-05,\n",
       "        -4.50962546e-04,  6.92818140e-04]])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "robust_cov[200,:,:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(300, 50)"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "spec_ret_nw.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "ui=np.zeros([N_T,N_stocks,N_stocks])\n",
    "for timestamp in range(N_T):\n",
    "    ui[timestamp,:,:]=np.diag(spec_ret_nw[timestamp,:])\n",
    "    \n",
    "model_covariance=xfx+ui\n",
    "np.save(\"./data/covariance_matrix.npy\", model_covariance)\n",
    "np.save(\"./data/return.npy\",R)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
